// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var css_detail_info = require("./exceptionDetailInfo.css");
	var databind = require("databind");
	var constant = require("constant");
	var inventoryId;
	var deliveryDetail;
	var handleState;
	var left;
	var noticeState;
	var recieveNoticeId;
	var init = function(page) {
		inventoryId = page.params.inventoryId;
		deliveryDetail = page.params.deliveryDetail;
		left = base.getUrlParam("left");
		noticeState = base.getUrlParam("noticeState");
		recieveNoticeId = page.params.recieveNoticeId;
		if(deliveryDetail==1){
			inquirePreDeliveryAbnormalInventory();
		}else{
			inquirePreTallyAbnormalInventory();
		}
		initEvent();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		//点击理货列表
		$(".newTaskList").unbind("click").click(function(){
			top.location.href = "../newTaskList/newTaskList.html?noticeState="+noticeState+"&left="+left;
		});
		
		//点击理货详情列表
		
		$(".newTaskDtail").unbind("click").click(function(){
		router.route("newTaskList/undoneCargoRecord.html?recieveNoticeId="+recieveNoticeId,true);
		});
		
		$("#exceptionDetailInfo .createAbnormalButton").unbind("click").click(function(){
			databind.submitDom($("#exceptionDetailInfo"),function(){
				base.alert(1,"上报成功！");
				history.go(-1);
			});
		});
		
	};
	
	//查询异常信息列表
	var inquirePreTallyAbnormalInventory = function() {
		base.fnAjax({
			action: "inquirePreTallyAbnormalInventory.json",
			oData: {
				inventoryId: inventoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					data.datas[0].inventoryId = inventoryId;
					databind.fillForm($("#exceptionDetailInfo"), data.datas[0]);
					handleState = data.datas[0].handleState;
					$(".exceptionRadio1").val(1);
					$(".exceptionRadio2").val(0);
					var tpl=$("#exceptionDetailBulkstockTpl").html();
					var html=juicer(tpl, data);
					$("#exceptionDetailBulkstockBody").html(html);
					
					$(".exceptionInventoryDetail").unbind("click").click(function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//查询异常信息列表
	var inquirePreDeliveryAbnormalInventory = function() {
		base.fnAjax({
			action: "inquirePreDeliveryAbnormalInventory.json",
			oData: {
				inventoryId: inventoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					data.datas[0].inventoryId = inventoryId;
					databind.fillForm($("#exceptionDetailInfo"), data.datas[0]);
					handleState = data.datas[0].handleState;
					$(".exceptionRadio1").val(1);
					$(".exceptionRadio2").val(0);
					var tpl=$("#exceptionDetailBulkstockTpl").html();
					var html=juicer(tpl, data);
					$("#exceptionDetailBulkstockBody").html(html);
					
					$(".exceptionInventoryDetail").unbind("click").click(function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});